package com.lill.system.serivce.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.lill.common.component.util.StringUtils;
import com.lill.system.constant.SystemConstant;
import com.lill.system.service.ICommonRedisService;

@Service("com.lill.system.serivce.impl.CommonRedisServiceImpl")
public class CommonRedisServiceImpl implements ICommonRedisService {

	@Resource(name="commonRedisTemplate")
    private RedisTemplate<String, String> redisStringTemplate;
	@Resource(name="commonRedisTemplate")
	private RedisTemplate<String, Integer> redisIntegerTemlate;
	@Resource(name="commonRedisTemplate")
	private RedisTemplate<String, Map<String, Object>> redisMapTemlate;
	@Resource(name="commonRedisTemplate")
    private RedisTemplate<String, List<String>> redisListStringTemplate;
	
	@Override
	public String getStringValueByKey(String key) {
		return redisStringTemplate.opsForValue().get(key);
	}

	@Override
	public void setKeyStringValueString(String key, String val) {
		redisStringTemplate.opsForValue().set(key, val);
	}

	@Override
	public void setKeyStringValueStringWithTime(String key, String val,
			long time, TimeUnit unit) {
		redisStringTemplate.opsForValue().set(key,val,time, unit);
	}
	
	@Override
	public Integer getIntegerValueByKey(String key) {
		return redisIntegerTemlate.opsForValue().get(key);
	}
	
	@Override
	public void setKeyStringValueInteger(String key, Integer val) {
		redisIntegerTemlate.opsForValue().set(key, val);
	}
	
	@Override
	public void setKeyStringValueIntegerWithTime(String key, Integer val,
			long time, TimeUnit unit) {
		redisIntegerTemlate.opsForValue().set(key,val,time, unit);
	}

	@Override
	public Map<String, Object> getMapValueByKey(String key) {
		return redisMapTemlate.opsForValue().get(key);
	}

	@Override
	public void setKeyStringValueMap(String key, Map<String, Object> val) {
		redisMapTemlate.opsForValue().set(key, val);
	}

	@Override
	public void setKeyStringValueMapWithTime(String key,
			Map<String, Object> val, long time, TimeUnit unit) {
		redisMapTemlate.opsForValue().set(key,val,time, unit);
	}
	
	@Override
	public void delDateByKeyString(String key) {
		redisMapTemlate.delete(key);
	}

	@Override
	public void deleteAppTokens(String authToken) {
		if(StringUtils.isEmpty(authToken)){
			authToken="*";
		}
		Set<String> keySets=redisMapTemlate.keys(SystemConstant.APP_TOKEN+authToken);
		if(null!=keySets&&!keySets.isEmpty()){
			redisMapTemlate.delete(keySets);
		}
	}

	@Override
	public void setKeyStringValueListStringWithTime(String key, List<String> val,long time, TimeUnit unit) {
		redisListStringTemplate.opsForValue().set(key,val,time, unit);
	}
}
